<?php
require_once 'Zend/Db/Table/Abstract.php';
class LoginDefaultModel extends Zend_Db_Table_Abstract
{
	protected $_name = 'admin';
	protected $_id = 'id';
	
	/*
	 * Ham xac thuc tai khoan
	 */
	public function login($username,$pass){
		$pass = base64_encode($pass);
	//	echo $username." user ".$pass;die;
		$uname = trim($username);
		$password = trim($pass);
		if($uname == "" || $password == ""){
			return false;
		}
		$result_check = 0;
		$db = Zend_Registry::get('connectDB');
		$auth = Zend_Auth::getInstance();
		
		$authAdapter = new Zend_Auth_Adapter_DbTable($db);
		$authAdapter->setTableName('teachers')
					->setIdentityColumn('username')
					->setCredentialColumn('password');
		$authAdapter->setIdentity($uname);
		$authAdapter->setCredential($password);
		$select = $authAdapter->getDbSelect();
		$result = $auth->authenticate($authAdapter);
		if($result->isValid()){
			$data = $authAdapter->getResultRowObject(null,null);
			$auth->getStorage()->write($data);
			return "Teacher";
		}
		else{
			$result_check = 1;
			
		}
		
		if($result_check == 1){
			$authAdapterStudent = new Zend_Auth_Adapter_DbTable($db);
			$authAdapterStudent->setTableName('students')
						->setIdentityColumn('username')
						->setCredentialColumn('password');
			$authAdapterStudent->setIdentity($uname);
			$authAdapterStudent->setCredential($password);
			$select = $authAdapterStudent->getDbSelect();
			$result = $auth->authenticate($authAdapterStudent);
			if($result->isValid()){
				$data = $authAdapterStudent->getResultRowObject(null,null);
				$auth->getStorage()->write($data);
				return "Student";
			}
			else{
				return false;
			}
		}
	}
}





